package com.kwan.xframe.mvp.view.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kwan.xframe.R;
import com.kwan.xframe.common.widget.indicator.SpacesItemDecoration;
import com.kwan.xframe.common.widget.observablescrollview.ObservableRecyclerView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


/**
 * 带RecycleView的Activity
 * Created by Mr.Kwan on 2017-2-24.
 */

public abstract class BaseCommonRecycleActivity extends BaseCommonActivity implements OnLoadMoreListener, OnRefreshListener {

	protected RefreshLayout refreshLayout;
	protected ObservableRecyclerView mRecyclerView;
	protected BaseQuickAdapter mAdapter;
	protected int pageIndex = 0;
	protected int pageSize = 10;
	protected boolean isLoadMore;
	protected boolean hasMoreData = true;
	public boolean isRefreshing;

	@Override
	protected int getMainViewId() {
		return R.layout.layout_base_recycle;
	}

	@Override
	protected void initViews() {
		super.initViews();

		refreshLayout = findViewById(R.id.refreshLayout);
		mRecyclerView = findViewById(R.id.recycleView);
		mRecyclerView.setLayoutManager(getLayoutManager());
		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mRecyclerView.setHasFixedSize(true);
		if (getItemDecoration() != null)
			mRecyclerView.addItemDecoration(getItemDecoration());
		//mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		//解决更新 闪烁
		mRecyclerView.setItemAnimator(null);
		mRecyclerView.setNestedScrollingEnabled(false);

		mAdapter = getAdapter();
		mAdapter.bindToRecyclerView(mRecyclerView);
		mAdapter.setEmptyView(R.layout.list_empty_view);


		//mRecyclerView.setAdapter(mAdapter);

		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setOnLoadMoreListener(this);

	}

	protected abstract RecyclerView.ItemDecoration getItemDecoration();

	protected void setRecyclerViewPadding(int padding, boolean isLeftRight) {
		mRecyclerView.addItemDecoration(new SpacesItemDecoration(padding, isLeftRight));
	}

	@Override
	protected void initData() {
		refreshLayout.autoRefresh();
	}

	protected abstract BaseQuickAdapter getAdapter();

	protected abstract RecyclerView.LayoutManager getLayoutManager();

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
		isLoadMore = true;
		isRefreshing = false;
		pageIndex++;
	}

	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) {
		isLoadMore = false;
		isRefreshing = true;
		pageIndex = 0;
	}
}
